package com.likianta.anykey;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.thinkcool.circletextimageview.CircleTextImageView;

import java.util.List;

/**
 * Created by Likianta_Dodoora on 2017/12/8 0008.
 */

public class DataAdapter extends RecyclerView.Adapter {
    
    private final int GROUP = 1;
    private final int CARD = 2;
    private List<CardData> cardDataList;
    private Context context;
    
    public DataAdapter(List<CardData> cardDataList) {
        this.cardDataList = cardDataList;
        /* groupList是一个两级嵌套结构 { groupName, cardList } ，举个例子：
         * cardDataList = [{ "未分类", [{ "卡片标题", "卡片摘要", "卡片头像", "卡片所属分类" }] },
         *              { "社交", [{ "QQ", "我的大号", "QQ头像", "社交类" }] },
         *              { "工具", [{ "坚果云", "用户 mm123", "用户头像", "工具类" }, { "新浪短网址", "无", "无", "sina头像", "工具类" }] },
         *              { "购物", [{ "淘宝", "小明 xm123", "小明头像", "购物类" }] }
         *             ]
         */
    }
    
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (context == null) {
            context = parent.getContext();
        }
        
        switch (viewType) {
            case GROUP:
                view = LayoutInflater.from(context).inflate(R.layout.group_type2, parent, false);
                return new GroupHolder(view);
            case CARD:
                view = LayoutInflater.from(context).inflate(R.layout.card_type1, parent, false);
                return new CardHolder(view);
        }
        
        return null;
    }
    
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int count = -1;
        
        for (int i = 0; i < cardDataList.size(); i++) { // enum groupList
            count++;
            if (position == count) {
                GroupHolder groupHolder = (GroupHolder) holder;
                
                // binding group_type2
                groupHolder.groupName.setText(cardDataList.get(i).getGroup().getGroupName());
            }
            
            for (int j = 0; j < cardDataList.get(i).getCardList().size(); j++) { // then enum cardList in the group_type2
                count++;
                if (position == count) {
                    Card card = cardDataList.get(i).getCardList().get(j);
                    CardHolder cardHolder = (CardHolder) holder;
                    
                    // binding cards
                    cardHolder.cardTitle.setText(card.getCardTitle());
                    cardHolder.cardSummary.setText(card.getCardSummary());
                    if (card.getCardHeadResId() == 0) { // text avatar
                        cardHolder.cardHead.setText(card.getCardHeadText());
                        cardHolder.cardHead.setTextSize(60);
                        cardHolder.cardHead.setTextColor(card.getCardHeadTextColor());
                        cardHolder.cardHead.setFillColor(card.getCardHeadFillColor());
                    } else { // image avatar
                        cardHolder.cardHead.setImageResource(card.getCardHeadResId());
                    }
                }
            }
        }
    }
    
    @Override
    public int getItemCount() {
        return getGroupCount() + getCardCount();
    }
    
    public int getGroupCount() {
        return cardDataList.size();
    }
    
    public int getCardCount() {
        int count = 0;
        for (int i = 0; i < cardDataList.size(); i++) {
            count += cardDataList.get(i).getCardList().size();
        }
        return count;
    }
    
    @Override
    public int getItemViewType(int position) { // getItemViewType的代码结构和onBind是一一对应的
        int count = -1;
        
        for (int i = 0; i < cardDataList.size(); i++) { // enum groupList
            count++;
            if (position == count) {
                return GROUP;
            }
            
            for (int j = 0; j < cardDataList.get(i).getCardList().size(); j++) { // enum cardList in group_type2
                count++;
                if (position == count) {
                    return CARD;
                }
            }
        }
        return super.getItemViewType(position);
    }
    
    public void add(int position, CardData cardData) {
        cardDataList.add(position, cardData);
        notifyItemInserted(position);
    }
    
    public void remove(Context context, int position) {
        if (position < 0) {
            Toast.makeText(context, "列表中并没有结果!", Toast.LENGTH_SHORT).show();
        } else {
            cardDataList.remove(position);
            notifyItemRemoved(position);
        }
    }
    
    public static class CardHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        TextView cardTitle;
        TextView cardSummary;
        CircleTextImageView cardHead;
        
        public CardHolder(View view) {
            super(view);
            cardView = (CardView) view;
            cardTitle = (TextView) view.findViewById(R.id.card_title);
            cardSummary = (TextView) view.findViewById(R.id.card_sum);
            cardHead = (CircleTextImageView) view.findViewById(R.id.card_head);
        }
    }
    
    public static class GroupHolder extends RecyclerView.ViewHolder {
        TextView groupName;
        
        public GroupHolder(View view) {
            super(view);
            groupName = (TextView) view;
        }
    }
}
